package zhibi.plugin.image.core;

import java.io.File;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;

/**
 * 缩略图片处理
 */
public class ThumbExec {


	/**
	 * 缩略图片
	 * @param filePath	原图地址
	 * @param thumbPath	缩略图地址
	 * @param width		缩放width
	 * @param height	缩放height
	 * @param isforce	是否按比例
	 * @param scale		按百分比缩放
	 * @param quality	图片质量
	 * @param rotate	旋转角度
	 * @return
	 */
	public static String thumb(String filePath, String thumbPath, int width,
			int height, boolean isforce, double scale, double quality, double rotate) {
		File img = new File(thumbPath);
		if (img.exists()) {
			return img.getPath();
		}
		Builder<File> f = Thumbnails.of(filePath);
		if (width > 0 && height <= 0) {// 只按width缩放
			f.width(width).keepAspectRatio(isforce);
		}
		if (width <= 0 && height > 0) {// 只按height缩放
			f.height(height).keepAspectRatio(isforce);
		}
		if (width > 0 && height > 0) {// 按照widthxheight缩放
			f.size(width, height).keepAspectRatio(isforce);
		}
		if (scale > 0.0D) {//按照比例缩放
			f.scale(scale);
		}
		if (quality > 0.0D) {//缩略图质量
			f.outputQuality(quality).outputFormat("jpg");
		}
		if (rotate > 0.0D) {//旋转角度
			f.rotate(rotate);
		}
		try {
			f.toFile(img);
			return img.getPath();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filePath;
	}
}
